package com.tianji.learning.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.tianji.learning.utils.TableNameContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.tianji.common.constants.Constant.POINTS_BOARD_TABLE_NAME;

@Configuration
public class MybatisConfig {

    @Bean
    public DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor(){
        Map<String, TableNameHandler> tableNameHandlerMap = new HashMap<>(1);
        // 设置老的表名
        tableNameHandlerMap.put(POINTS_BOARD_TABLE_NAME, new TableNameHandler() {
            @Override
            // tableName 老的表名
            public String dynamicTableName(String sql, String tableName) {
                return TableNameContext.getTableName();
            }
        });
        return new DynamicTableNameInnerInterceptor(tableNameHandlerMap);
    }
}
